/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 1, or (at your option)
** any later version.

** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.

** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef __IA_MAIN_WINDOW__
#define __IA_MAIN_WINDOW__

#include <vector>
#include <string>

#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QGroupBox>
#include <QTableView>
#include <QGridLayout>
#include <QIcon>
#include <QAction>
#include <QSettings>
#include <QStringList>
#include <QMap>
#include <QCloseEvent>
#include <QTimer>

#include "ia_indicator.h"
#include "ia_tray_icon.h"
#include "ia_auth_c.h"
#include "ia_stats_model.h"

class IAMainWindow : public QWidget {

    Q_OBJECT

public:
    IAMainWindow(QWidget * parent = 0, Qt::WindowFlags flags = 0);
    virtual ~IAMainWindow();

public slots:
    void toggleConnect(bool state);
    void onSettings();
    void onErrorOccured(const QString & errorMessage);
    void onConnect();
    void onDisconnect();
    void onHide();
    void onShow();
    void onTrayActivated(QSystemTrayIcon::ActivationReason reason);
    void onMessage(const QString & message);

    void setState(bool state);

signals:
    void stateChanged(bool state);
    void errorOccured(const QString & errorMessage);
    void incomingMessage(const QString & message);

protected:
    void closeEvent(QCloseEvent * event);

private:
    QHBoxLayout * mainLayout;
    QVBoxLayout * leftPanel;
    QLabel * loginLbl;
    QLineEdit * loginFld;
    QLabel * passwordLbl;
    QLineEdit * passwordFld;
    QPushButton * connectBtn;
    QPushButton * settingsBtn;
    QGroupBox * stateGroup;
    QVBoxLayout * rightPanel;
    QTableView * stateTbl;
    IAIndicator * ind;
    QGridLayout * downPanel;
    QLabel * freeMbLbl;
    QLabel * cashLbl;
    QLabel * freeMbValueLbl;
    QLabel * cashValueLbl;
    IATrayIcon * sysTrayIcon;

    QIcon logoIcon;
    QIcon onlineIcon;
    QIcon offlineIcon;

    QAction * connectAction;
    QAction * disconnectAction;
    QAction * settingsAction;
    QAction * hideAction;
    QAction * showAction;
    QAction * quitAction;

    QSettings settings;
    QStringList dirNames;
    QMap<QString, int> dirIndex;

    IAStatsModel * statsModel;

    IA_CLIENT_PROT * proto;

    bool disconnectedByUser;
    bool firstTime;

    void iaConnect();
    void iaDisconnect();
    void changeState(bool state, const std::string & errorMessage = "");
    void changeStats(const LOADSTAT & stat);
    void updateDirNames();
    void createActions();
    void createTray();
    void displayMessage(const QString & message);

    static void onStatusChanged(int status, void * data);
    static void onStatChanged(const LOADSTAT & stat, void * data);
    static void onInfo(const std::string & message, int infoType, int showTime, int sendTime, void * data);
    static void onError(const std::string & message, int netError, void * data);
    static void onDirNames(const std::vector<std::string> & dirNames, void * data);

friend class IA_CLIENT_PROT;
};

#endif
